<template>
  <div class="app-container" v-loading="loading">
    <el-form :model="formData" :rules="rules" ref="validateForm" label-width="140px">
      <el-form-item label="资质图片" prop="image">
        <upload :limit="1" v-model="formData.image" ref="upload" :multiple="false"></upload>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input v-model="formData.description"></el-input>
      </el-form-item>
      <el-form-item label="外链" prop="link">
        <el-input v-model="formData.link"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('validateForm')">提交</el-button>
        <el-button @click="resetForm('validateForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import Upload from "@/components/Global/Upload"
import { getDetail, create, update } from '@/api/press'
import { config } from './config'
export default {
  name: 'PressCreateUpdate',
  components: {
    Upload
  },
  data() {
    return {
      loading: false,
      querys: {
        id: '',
      },
      formData: {
        id: '',
        image: '',
        description: '',
        link: '',
      },
      rules: {
        image: [
          { required: true, message: '请上传图片', trigger: 'blur' },
        ],
      }
    }
  },
  created() {
    const querys = this.$route.query || {}
    this.querys = querys
    if (this.querys.id) {
      this.getDetail()
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (!valid) {
          return false
        }
        this.postForm()
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    // 添加/编辑
    postForm() {
      this.loading = true;
      let cmd = this.querys.id ? update : create;
      cmd(this.formData).then(() => {
        this.loading = false
        this.$message({
          message: '恭喜，操作成功',
          type: 'success',
          duration: 1000,
        })
        setTimeout(() => {
          this.$router.push(`${config.routePath}list`)
        }, 1000)
      })
    },
    // 详情
    getDetail() {
      this.loading = true
      getDetail(this.querys.id).then(res => {
        this.loading = false
        this.formData = res
      })
    },
  },
}
</script>
